package com.zoshine.zzg.controller;

import com.github.pagehelper.PageInfo;
import com.zoshine.zzg.base.BaseResponse;
import com.zoshine.zzg.pojo.ImportExport;
import com.zoshine.zzg.respons.AppResponse;
import com.zoshine.zzg.service.ImportExportService;
import com.zoshine.zzg.service.ReleaseSlipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 导入导出控制层
 *
 * @author 贾俊峰
 * @time 2017/7/28 14:54
 */
@RestController
@RequestMapping(value = "importExport", method = RequestMethod.POST)
public class ImportExportController extends BaseController {
    private ImportExportService importExportService;
    private ReleaseSlipService releaseSlipService;
//    private GoodsReturnService goodsReturnService;
    @Autowired
    public void setImportExportService(ImportExportService importExportService) {
        this.importExportService = importExportService;
    }
    @Autowired
    public void setReleaseSlipService(ReleaseSlipService releaseSlipService) {
        this.releaseSlipService = releaseSlipService;
    }
//    @Autowired
//    public void setGoodsReturnService(GoodsReturnService goodsReturnService) {
//        this.goodsReturnService = goodsReturnService;
//    }

    @RequestMapping("pageImportExport")
    public BaseResponse pageImportExport(ImportExport importExport) {
        List<ImportExport> list = importExportService.list(importExport);
        return new AppResponse(new PageInfo<>(list));
    }

    /**
     * 数据导出
     */
    @RequestMapping(value = "export", method = RequestMethod.GET)
    public void exportData(ImportExport importExport) throws Exception {
        importExport.setCreateUser(getLoginUserId());
        importExportService.exportData(importExport, response);
    }

    /**
     * 数据导入
     */
    @RequestMapping("import")
    public BaseResponse importData(MultipartFile file) throws Exception {
        importExportService.importData(file, getLoginUser());
        if (importExportService.getEnvironmentCode() == ImportExport.INNER) {
            // 内网环境，计算发放单
            releaseSlipService.addReleaseSlip();
        }
        return AppResponse.OK;
    }

}
